<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body,
        html {
            margin: 0 auto;
            padding: 0;
            width: 100%;
            height: 100%;
            /* background-color: lightseagreen; */
            overflow: hidden;
        }

        * {
            box-sizing: border-box;
        }

        .layout {
            height: 100%;
            width: 100%;
            display: -webkit-flex;
            display: flex;
            flex-direction: row;
            position: relative;
        }

        .layout-side {
            height: 100%;
            width: 100px;
            background-color: white;
            border-right: 1px lightseagreen
        }

        .layout-main {
            height: 100%;
            background-color: white;
            flex-grow: 1;
        }

        .img_each_container {
            margin-top: 1px;
            background-color: rgb(189, 230, 227);
        }

        .debug_tools_container {
            width: 400px;
            height: 50px;
            line-height: 50px;
            padding: 0px 10px;
            background-color: lightslategray;
            position: relative;
            bottom: 60px;
            margin: 0 auto;
        }

        .debug_tools_container a {
            padding: 4px 10px;
            background-color: lightseagreen;
            color: white;
        }

        #myCanvas {
            margin: 0 auto;
            background: lightslategray;
        }
    </style>
    <script src="https://cdn.bootcss.com/fabric.js/3.2.0/fabric.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>

    <div class="layout">

        <div class="layout-side">
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>
            <div class="img_each_container">
                <img src="中.png" id="img">
            </div>


        </div>
        <div class="layout-main">
            <canvas id="myCanvas"></canvas>
        </div>




    </div>

    <div class="debug_tools_container">
        <a href="###" id="getJson">保存</a>
        <a href="###" id="preShow">预览</a>
        <a href="###" id="loadJson">返渲染</a>
    </div>

    <script>
        function printf() {
            console.log(arguments);
        }

        var windowOption = {
            currentCanvas: {
                width: 0,
                height: 0
            }
        };

        window.onload = function () {


        }

        $(function () {



            windowOption.currentCanvas.width = $(".layout-main").width();
            windowOption.currentCanvas.height = $(".layout-main").height();

            printf(windowOption, "==========windowOption=========");

            var c = document.getElementById("myCanvas");

            c.width = 800;
            c.height = 500;

            c.style.marginTop = windowOption.currentCanvas.height / 2 - c.height / 2 + "px";
            c.style.marginLeft = windowOption.currentCanvas.width / 2 - c.width / 2 + "px";

            var canvas = new fabric.Canvas('myCanvas');
            canvas.width = c.width;
            canvas.height = c.height;

            var rect = new fabric.Rect({
                top: 0,
                left: 0,
                width: canvas.width,
                height: canvas.height,
                fill: 'blue'
            });

            canvas.add(rect);


            var rect = new fabric.Rect({
                top: 0,
                left: 0,
                width: 100,
                height: 70,
                fill: 'red'
            });

            canvas.add(rect);

            var imgElement = document.getElementById('img'); //声明我们的图片
            var imgInstance = new fabric.Image(imgElement, { //设置图片在canvas中的位置和样子
                left: 300,
                top: 100,
                width: 100,
                height: 100,
                angle: 0 //设置旋转
            });

            canvas.add(imgInstance); //加入到canvas中

            $(".layout-main").bind('mouseenter', function (e) {

                var imgUrl = $(e.relatedTarget).attr('src');

                var fromResData = {
                    dom: e.relatedTarget,
                    width: $(e.relatedTarget).width(),
                    height: $(e.relatedTarget).height(),
                }

                var img = new Image();
                img.onload = function () {
                    var imgInstance = new fabric.Image(imgElement, { //设置图片在canvas中的位置和样子
                        left: e.offsetX - fromResData.width / 2,
                        top: e.offsetY - fromResData.height / 2,
                        width: 100,
                        height: 100,
                        angle: 0 //设置旋转
                    });
                    canvas.add(imgInstance); //加入到canvas中
                }
                img.src = imgUrl;
            })


            const textbox = new fabric.Textbox('这是一段文字', {
                left: 150,
                top: 50,
                width: 150,
                fontSize: 20, // 字体大小
                fontWeight: 800, // 字体粗细
                fill: 'red', // 字体颜色
                fontStyle: 'italic', // 斜体
                fontFamily: 'Delicious', // 设置字体
                // stroke: 'green', // 描边颜色
                strokeWidth: 3, // 描边宽度
                hasControls: false,
                borderColor: 'orange',
                editingBorderColor: 'blue' // 点击文字进入编辑状态时的边框颜色
            });
            // 添加文字后，如下图
            canvas.add(textbox);

            fabric.Image.fromURL('http://localhost:8881/hw/中.png', function (oImg) {

                oImg.width = 100;
                oImg.height = 100;
                oImg.top = 400;
                oImg.left = 600;

                canvas.add(oImg)
            });



            $("#getJson").click(function () {
                // 导出当前画布信息
                const json = canvas.toJSON(); // 导出的Json如下图

                console.log(currState, "=======currState==========");

                var currState = {
                    canvas_json: json,
                    canvas_width: c.width,
                    canvas_height: c.height
                }

                localStorage.setItem("currState", JSON.stringify(currState));
            });


            $("#loadJson").click(function () {
                var currState = localStorage.getItem("currState");
                // 加载画布信息


                
                canvas.loadFromJSON(currState.json, () => {

                    canvas.setZoom(540 / 1920);

                    canvas.renderAll();
                });

            });


            $("#preShow").click(function () {
                

            });






        });
    </script>

</body>

</html>